package com.dhcc.test.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * Servlet Filter implementation class EcodeFilter
 */
public class EcodingFilter implements Filter {
    private FilterConfig config;
    /**
     * Default constructor. 
     */
    public EcodingFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
	    String encoding = config.getInitParameter("encoding");
	    if (null != encoding && !"".equals(encoding)) {
	        request.setCharacterEncoding(encoding);
	        response.setCharacterEncoding(encoding);
	        response.setContentType("text/html;charset=UTF-8");
	    }

		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @param config 
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig config) throws ServletException {
	    this.config = config;
	}

}
